package com.sport.facilities.booking.system;

import android.app.Activity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class ControlActivity extends Activity implements OnClickListener,
		OnGestureListener {

	// LOG
	private static final String TAG = "ControlActivity";

	// OnFing settings
	private static final int SWIPE_MIN_DISTANCE = 120;
	private static final int SWIPE_MAX_OFF_PATH = 250;
	private static final int SWIPE_THRESHOLD_VELOCITY = 100;

	// Motion Detector
	protected GestureDetector detector;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.detector.onTouchEvent(event);
	}

	public boolean onDown(MotionEvent e) {
		// Log.d(TAG, "---onDown--- " + e.toString());
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		Log.d(TAG, "---onFling--- ");
		ViewFlipper vf = (ViewFlipper) findViewById(R.id.layout_flipper);
		try {
			if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
				return false;
			}
			if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				// right to left swipe
				Log.d(TAG, "---onFling--- RIGHT TO LEFT");
				vf.setInAnimation(AnimationUtils.loadAnimation(
						getApplicationContext(), R.anim.slide_in_right));
				vf.showNext();

			} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				// left to right flip
				Log.d(TAG, "---onFling--- LEFT TO RIGHT");
				vf.setInAnimation(AnimationUtils.loadAnimation(
						getApplicationContext(), android.R.anim.slide_in_left));
				vf.showPrevious();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public void onLongPress(MotionEvent e) {
		// Log.d(TAG, "---onDown--- " + e.toString());
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// Log.d(TAG, "---onScroll--- " + e1.toString());
		return false;
	}

	public void onShowPress(MotionEvent e) {
		// Log.v(TAG, "---onShowPress--- " + e.toString());
	}

	public boolean onSingleTapUp(MotionEvent e) {
		// Log.v(TAG, "---onSingleTapUp--- " + e.toString());
		return false;
	}

	public void onClick(View arg0) {
		// Log.v(TAG, "---onClick--- " + arg0.toString());
	}

}
